package main

import (
	"context"
	"errors"
	"time"

	"github.com/rs/zerolog/log"
	"github.com/sysadminsmedia/homebox/backend/internal/core/services"
)

func (a *app) SetupDemo() error {
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	registration := services.UserRegistration{
		Email:    "demo@example.com",
		Name:     "Demo",
		Password: "demo",
	}

	// First check if we've already setup a demo user and skip if so
	log.Debug().Msg("Checking if demo user already exists")
	_, err := a.services.User.Login(ctx, registration.Email, registration.Password, false)
	if err == nil {
		log.Info().Msg("Demo user already exists, skipping setup")
		return nil
	}

	log.Debug().Msg("Demo user does not exist, setting up demo")
	_, err = a.services.User.RegisterUser(ctx, registration)
	if err != nil {
		log.Err(err).Msg("Failed to register demo user")
		return errors.New("failed to setup demo")
	}

	log.Info().Msg("Demo setup complete")

	return nil
}
